package tech.summerly.weather.model

import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
import android.arch.persistence.room.TypeConverters
import android.os.Parcel
import android.os.Parcelable
import tech.summerly.weather.util.Position
import tech.summerly.weather.util.PositionConverter


@Entity
@TypeConverters(PositionConverter::class)
data class Region(

        /**
         * 区域全名
         * 比如：湖南省长沙市雨花区
         */
        @PrimaryKey
        val fullName: String,

        /**
         * 区域名
         * 如：雨花区
         */
        val name: String,

        /**
         * 当前区域经纬度
         */
        val position: Position,

        /**
         * 当前区域所在省
         */
        val province: String,

        /**
         * 城市名
         * 比如 雨花区的城市名为长沙市
         * 比如 北京市的城市名为北京市
         */
        val city: String
) : Parcelable {
    constructor(source: Parcel) : this(
            source.readString(),
            source.readString(),
            source.readParcelable<Position>(Position::class.java.classLoader),
            source.readString(),
            source.readString()
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(fullName)
        writeString(name)
        writeParcelable(position, 0)
        writeString(province)
        writeString(city)
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Region> = object : Parcelable.Creator<Region> {
            override fun createFromParcel(source: Parcel): Region = Region(source)
            override fun newArray(size: Int): Array<Region?> = arrayOfNulls(size)
        }
    }
}